package com.vf.admin.common.freemarker;

import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

@Component
public class MapIntString implements TemplateDirectiveModel {

    @Override
    public void execute(Environment environment, Map params, TemplateModel[] loopVars,
                        TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        Map map = null;
        int key = 0;
        if (params != null && params.get("map") != null) {
            SimpleHash simHash = (params.get("map") !=null ? (SimpleHash)(params.get("map")) : new SimpleHash());
            map = simHash.toMap();
        }
        if (params != null && params.get("key") != null) {
            key = (params.get("key") ==null || params.get("key").toString().trim().length()<=0 ? 0 : Integer.valueOf(params.get("key").toString()));
        }
        String value = (map == null || map.get(key) == null || map.get(key).toString().trim().length()<=0 ? "" : map.get(key).toString());
        // 真正开始处理输出内容
        Writer out = environment.getOut();
        out.write(value);

//        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
//        //此处的myTag就是返回给前端的数据
//        Map<String, Object> result = new HashMap<String, Object>(2);
//        result.put();
//        environment.setVariable("mapIntString", builder.build().wrap(result));
//        templateDirectiveBody.render(environment.getOut());

    }
}